package com.yl.chainStore.payrule.item

import com.yl.chainStore.entity.pay.PayCustomerOrder
import com.yl.chainStore.entity.pay.PayCustomerOrderDetail
import com.yl.chainStore.payrule.common.RuleField

/**
 * 选择项目相关
 */
open class UseItemForSelectRule (

	@RuleField(viewName = "参与项目类型",must = 0)
	val participationTypeIds :List<Long> ?= null,

	@RuleField(viewName = "参与项目",must = 0)
	val participationItemIds :List<Long> ?= null,

	@RuleField(viewName = "不参与项目类型",must = 0)
	val nonParticipationTypeIds :List<Long> ?= null,

	@RuleField(viewName = "不参与项目类型",must = 0)
	val nonParticipationItemIds :List<Long> ?= null,

){
	/**
	 * 查找项目优惠使用的订单明细
	 */
	fun getUseForOrderDetails(order: PayCustomerOrder):Set<PayCustomerOrderDetail>{
		if(order.details.isNullOrEmpty()){
			return mutableSetOf()
		}
		val details = mutableSetOf<PayCustomerOrderDetail>()
		if(participationTypeIds.isNullOrEmpty() && participationItemIds.isNullOrEmpty()){
			for(detail in order.details!!){
				if(detail.gift == 0){
					details.add(detail)
				}
			}
		}
		//有的类型加进去
		if(participationTypeIds != null && participationTypeIds.isNotEmpty()){
			for(detail in order.details!!){
				if(participationTypeIds.contains(detail.typeId) ){
					if(detail.gift == 0){
						details.add(detail)
					}
				}
			}
		}
		//不参与的类型减出去
		if(nonParticipationTypeIds != null && nonParticipationTypeIds.isNotEmpty()){
			if(details.isNotEmpty()){
				details.removeIf {
					nonParticipationTypeIds.contains(it.typeId)
				}
			}

		}
		//参与的项目加进去
		if(participationItemIds != null && participationItemIds.isNotEmpty()){
			for(detail in order.details!!){
				if(participationItemIds.contains(detail.itemId) ){
					if(detail.gift == 0){
						details.add(detail)
					}
				}
			}
		}

		//不参与的项目减出去
		if(nonParticipationItemIds != null && nonParticipationItemIds.isNotEmpty()){
			details.removeIf {
				nonParticipationItemIds.contains(it.itemId)
			}
		}

		return details
	}


}
